﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Windows;
using System.Windows.Data;
using Microsoft.Practices.Prism.Commands;
using ParaEditor.Common;
using ParaEditor.DAO.ParaDAO;
using ParaEditor.Desktop.Interfaces;
using ParaEditor.Domain.Entities;
using ParaEditor.Infrastructure;

namespace ParaEditor.Desktop.ViewModel
{
    [Export(typeof(IPara4015ViewModel))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class Para4015ViewModel : ViewModelBase, IPara4015ViewModel
    {
        private Para4015DAO paramDao;

        private ICollectionView infoVerList;
        public ICollectionView InfoVerList
        {
            get
            {
                return infoVerList;
            }
            set
            {
                this.infoVerList = value;
                this.RaisePropertyChangedEvent("InfoVerList");
            }
        }

        private ICollectionView paraCollectionDetail;
        public ICollectionView ParaCollectionDetail
        {
            get
            {
                return this.paraCollectionDetail;
            }
            set
            {
                this.paraCollectionDetail = value;
                this.RaisePropertyChangedEvent("ParaCollectionDetail");
            }
        }

        private ICollectionView currentTicketType;
        public ICollectionView CurrentTicketType
        {
            get
            {
                return currentTicketType;
            }
            set
            {
                this.currentTicketType = value;
                this.RaisePropertyChangedEvent("CurrentTicketType");
            }
        }

        private Visibility editGroupBoxVisible;
        public Visibility EditGroupBoxVisible
        {
            get
            {
                return this.editGroupBoxVisible;
            }
            set
            {
                this.editGroupBoxVisible = value;
                this.RaisePropertyChangedEvent("EditGroupBoxVisible");
            }
        }

        private short cmccTicketType;
        public short CmccTicketType
        {
            get
            {
                return this.cmccTicketType;
            }
            set
            {
                this.cmccTicketType = value;
                this.RaisePropertyChangedEvent("CmccTicketType");
            }
        }

        private string cityCode;
        public string CityCode
        {
            get
            {
                return this.cityCode;
            }
            set
            {
                this.cityCode = value;
                this.RaisePropertyChangedEvent("CityCode");
            }
        }

        //Button
        public DelegateCommand AddParamCommand { get; private set; }
        public DelegateCommand DelParamCommand { get; private set; }
        public DelegateCommand SaveParamCommand { get; private set; }
        public DelegateCommand CanclEditParamCommand { get; private set; }
        public DelegateCommand NewVersionCommand { get; private set; }
        public DelegateCommand CopyCurrentVersionCommand { get; private set; }

        [ImportingConstructor]
        public Para4015ViewModel()
        {
            paramDao = new Para4015DAO();
            EditGroupBoxVisible = Visibility.Hidden;
            InfoVerList = CollectionViewSource.GetDefaultView(paramDao.GetAllVersion());
            if (paramDao.GetAllVersion().Count == 0)
            {
                Microsoft.Windows.Controls.MessageBox.Show("当前参数无任何参数版本");
            }
            InfoVerList.CurrentChanged += InfoVerList_CurrentChanged;

            //Button
            this.AddParamCommand = new DelegateCommand(this.AddParam);
            this.SaveParamCommand = new DelegateCommand(this.SaveParam);
            this.CanclEditParamCommand = new DelegateCommand(this.CanclEditParam);
            this.DelParamCommand = new DelegateCommand(this.DelParam);
            this.NewVersionCommand = new DelegateCommand(this.NewVersion);
            this.CopyCurrentVersionCommand = new DelegateCommand(this.CopyCurrentVersion);
        }

        void InfoVerList_CurrentChanged(object sender, System.EventArgs ep)
        {
            if (InfoVerList.CurrentItem != null)
            {
                ReflashDataGrid();
            }
        }

        private void ReflashDataGrid()
        {
            int infoVer = (int)InfoVerList.CurrentItem;
            ParaCollectionDetail = CollectionViewSource.GetDefaultView(paramDao.GetObjectByInfoVer(infoVer));
        }

        private void AddParam()
        {
            EditGroupBoxVisible = Visibility.Visible;
            ObservableCollection<TicketType> tickeTypeCollection = new ObservableCollection<TicketType>();
            tickeTypeCollection = paramDao.GetAllTicketType();
            CurrentTicketType = CollectionViewSource.GetDefaultView(new ObservableCollection<TicketType>(tickeTypeCollection));
        }

        private void NewVersion()
        {
            ObservableCollection<int> infoVerCollection = new ObservableCollection<int>();
            infoVerCollection = paramDao.GetAllVersion();
            int newVersion = paramDao.GetMaxID() + 1;
            infoVerCollection.Add(newVersion);
            InfoVerList = CollectionViewSource.GetDefaultView(infoVerCollection);
            InfoVerList.CurrentChanged += InfoVerList_CurrentChanged;
        }

        private void CopyCurrentVersion()
        {
            //查询当前版本
            ObservableCollection<Para4015> currentVersionParam = new ObservableCollection<Para4015>();
            int infoVer = (int)InfoVerList.CurrentItem;
            currentVersionParam = paramDao.GetObjectByInfoVer(infoVer);

            if (currentVersionParam.Count != 0)
            {
                Para4015 param = null;
                Para4015Pk paramPk = null;
                int newVersion = paramDao.GetMaxID() + 1;
                foreach (Para4015 item in currentVersionParam)
                {
                    param = new Para4015();
                    paramPk = new Para4015Pk();
                    //编辑参数
                    paramPk.infoVer = Int16.Parse(newVersion.ToString());
                    paramPk.cityCode = item.para4015Pk.cityCode;
                    paramPk.ticketType = item.para4015Pk.ticketType;
                    paramPk.cmccType = item.para4015Pk.cmccType;

                    param.para4015Pk = paramPk;

                    ResultValue result = paramDao.CreateObject(param);
                    if (result.Code != 0)
                    {
                        Microsoft.Windows.Controls.MessageBox.Show("复制过程中出现错误!错误信息：" + result.Msg);
                    }
                }
                //刷新
                ParaCollectionDetail = CollectionViewSource.GetDefaultView(paramDao.GetObjectByInfoVer(newVersion));
                InfoVerList = CollectionViewSource.GetDefaultView(paramDao.GetAllVersion());
                InfoVerList.CurrentChanged += InfoVerList_CurrentChanged;
            }
            else
            {
                Microsoft.Windows.Controls.MessageBox.Show("所查询的当前版本没有数据,不可进行复制");
            }
        }

        private void DelParam()
        {
            try
            {
                if (ParaCollectionDetail.CurrentItem != null)
                {
                    Para4015 param = ParaCollectionDetail.CurrentItem as Para4015;
                    if (Microsoft.Windows.Controls.MessageBox.Show("确认删除所选择的参数！", "4015参数", System.Windows.MessageBoxButton.OKCancel, System.Windows.MessageBoxImage.Question) == System.Windows.MessageBoxResult.OK)
                    {
                        //删除
                        ResultValue result = paramDao.DeleteObject(param,param.para4015Pk.infoVer);
                        if (result.Code == 0)
                        {
                            Microsoft.Windows.Controls.MessageBox.Show("删除成功");
                            //刷新
                            InfoVerList = CollectionViewSource.GetDefaultView(paramDao.GetAllVersion());
                            InfoVerList.CurrentChanged += InfoVerList_CurrentChanged;

                            ReflashDataGrid();

                        }
                        else
                        {
                            Microsoft.Windows.Controls.MessageBox.Show("删除失败!发生错误：" + result.Msg);
                        }
                    }
                }
                else
                {
                    Microsoft.Windows.Controls.MessageBox.Show("未选择要删除的参数");
                }
            }
            catch (Exception e)
            {
            }
        }

        private void CanclEditParam()
        {
            this.EditGroupBoxVisible = Visibility.Hidden;
            //将控件置为空
            CurrentTicketType = null;
        }

        public void SaveParam()
        {
            if (Confirm())
            {
                Para4015 param = new Para4015();
                Para4015Pk paramPk = new Para4015Pk();
                int tmp = tmp = (CurrentTicketType.CurrentItem as TicketType).TicketID;
                paramPk.ticketType = Int16.Parse(tmp.ToString());
                tmp = (int)InfoVerList.CurrentItem;
                paramPk.infoVer = Int16.Parse(tmp.ToString());
                paramPk.cmccType = this.CmccTicketType;
                paramPk.cityCode = this.CityCode;
                param.para4015Pk = paramPk;
                ResultValue result = paramDao.CreateObject(param);
                if (result.Code == 0)
                {
                    Microsoft.Windows.Controls.MessageBox.Show("保存成功");
                    ReflashDataGrid();

                    CanclEditParam();
                }
                else
                {
                    Microsoft.Windows.Controls.MessageBox.Show("保存失败！发生错误:" + result.Msg);
                }
            }

        }

        private bool Confirm()
        {
            bool flag = false;
            if (this.InfoVerList.CurrentItem != null)
            {
                if (this.CurrentTicketType.CurrentItem != null && this.InfoVerList.CurrentItem != null && this.CityCode != null)
                {
                    if (!this.CityCode.Trim().Equals(""))
                    {
                        flag = true;
                    }
                    else
                    {
                        Microsoft.Windows.Controls.MessageBox.Show("城市代码未填写");
                        flag = false;
                    }
                }
                else
                {
                    Microsoft.Windows.Controls.MessageBox.Show("所填信息不完整");
                    flag = false;
                }
            }
            else
            {
                Microsoft.Windows.Controls.MessageBox.Show("缺少版本号");
                flag = false;

            }
            
            return flag;
        }
    }
}
